package com.sky.controller.admin;

import com.sky.constant.JwtClaimsConstant;
import com.sky.dto.EmployeeDTO;
import com.sky.dto.EmployeeLoginDTO;
import com.sky.dto.EmployeePageQueryDTO;
import com.sky.entity.Employee;
import com.sky.properties.JwtProperties;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.EmployeeService;
import com.sky.utils.JwtUtil;
import com.sky.vo.EmployeeLoginVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

/**
 * 员工管理
 */
@RestController
@RequestMapping("/admin/employee")
@Slf4j
@Api(tags = "员工管理模块")
public class EmployeeController {

    @Autowired
    private EmployeeService employeeService;
    @Autowired
    private JwtProperties jwtProperties;

    /**
     * http://localhost/api/employee/login
     * 登录
     *
     * @param employeeLoginDTO
     * @return
     */
    @PostMapping("/login")
    @ApiOperation("登录")
    public Result<EmployeeLoginVO> login(@RequestBody EmployeeLoginDTO employeeLoginDTO) {
        log.info("员工登录：{}", employeeLoginDTO);

        Employee employee = employeeService.login(employeeLoginDTO); //CTRL +ALT+ 鼠标左键


        //登录成功后，生成jwt令牌
        Map<String, Object> claims = new HashMap<>();

        claims.put(JwtClaimsConstant.EMP_ID, employee.getId());

        String token = JwtUtil.createJWT(
                jwtProperties.getAdminSecretKey(), // 秘钥
                jwtProperties.getAdminTtl(),
                claims);


        EmployeeLoginVO employeeLoginVO = EmployeeLoginVO.builder()
                .id(employee.getId())
                .userName(employee.getUsername())
                .name(employee.getName())
                .token(token)
                .build();

        return Result.success(employeeLoginVO);
    }

    /**
     * 退出
     *
     * @return
     */
    @PostMapping("/logout")
    @ApiOperation("退出")
    public Result<String> logout() {
        return Result.success();
    }

    /**
     * 新增员工
     *
     * http:xxx/8080?name=zhangsan
     * @RequestParam
     * http:xxx/8080/zhangan
     * @PathVariable
     * JSON
     * @RequestBody
     * pojo
             * DTO  : 接受参数 :请求参数很多,而且 Entity 不满足的情况下
             * Entity: 和数据库对应
             * VO  : 返回数据用的, 当Entity 不满足时一般才考虑使用VO

     */
    @PostMapping
    @ApiOperation("新增")
    public  Result save(@RequestBody EmployeeDTO dto){
        log.info("新增员工:{}",dto);
        employeeService.save(dto);
        return Result.success();
    }

    /**
     * 分页查询
     * @param dto
     * @return
     */
    @GetMapping("/page")
    @ApiOperation("分页查询")
    public Result<PageResult> page( EmployeePageQueryDTO dto){
       PageResult result = employeeService.page(dto);
       return Result.success(result);
    }

    /**
     * 更新状态
     * @param status 1 启动, 0 禁用
     * @param id
     http://:8080//admin/employee/status/1?id=8
     * @return
     */
    @ApiOperation("修改状态")
    @PostMapping("/status/{status}")
    public Result updateStatus(@PathVariable  Integer status,Long id){
        employeeService.updateStatus(status,id);
        return  Result.success();
    }

    /**
     * 根据id 查询
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    @ApiOperation("根据id 查询")
    public Result<Employee> getById(@PathVariable  Long id){
        Employee employee = employeeService.getById(id);
        employee.setPassword("******");
        return Result.success(employee);
    }


    /**
     * 修改员工
     */
    @PutMapping
    @ApiOperation("修改")
    public  Result update(@RequestBody EmployeeDTO dto){
        log.info("新增员工:{}",dto);
        employeeService.update(dto);
        return Result.success();
    }








}
